home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Dots & Pixels / headers / phaser.h < prev    next >
C/C++ Source or Header  |  1995-09-29  |  2KB  |  65 lines

  1. #pragma once
  2. //
  3. // class phaser is used to implement limited-lifetime. The idea is that
  4. // to implement lifetime of say 10 one has to 'kill off' one tenth of
  5. // the population at every timestep. Example code:
  6. //
  7. //    phaser klingon( 10);
  8. //    for( int time = 0; time < timesteps_in_simulation)
  9. //    {
  10. //        (void)klingon.major_step();
  11. //        for( int particle_no = 0; particle_no < particles_in_simulation)
  12. //        {
  13. //            if( klingon.minor_step() == 0)
  14. //            {
  15. //                kill_particle( the_particles[ particle_no]);
  16. //                make_new_particle( the_particles[ particle_no]);
  17. //            } else {
  18. //                step_particle( the_particles[ particle_no]);
  19. //            }
  20. //        }
  21. //    }
  22. //
  23. // Note: we are using two counters which cycle through 0 ... length - 1
  24. // since any other way will wreak havoc if the number of particles and
  25. // the cycle length have a gcd greater than one (well, I can think of
  26. // ways to get away with only one counter, using a large prime, but
  27. // I'm not 100% sure that that will always work, and it would only
  28. // make the memory requirements sizeof( unsigned int) lower)
  29. //
  30. class phaser
  31. {
  32.     public:
  33.         phaser( unsigned int length);
  34.     
  35.         unsigned int major_step();
  36.         unsigned int minor_step();
  37.  
  38.         const unsigned int cycle_length;
  39.  
  40.     private:
  41.         unsigned int major_phase;
  42.         unsigned int minor_phase;
  43. };
  44.  
  45. inline unsigned int phaser::major_step()
  46. {
  47.     if( major_phase == 0)
  48.     {
  49.         major_phase = cycle_length;
  50.     }
  51.     major_phase -= 1;
  52.     minor_phase = major_phase;
  53.     return major_phase;
  54. }
  55.  
  56. inline unsigned int phaser::minor_step()
  57. {
  58.     if( minor_phase == 0)
  59.     {
  60.         minor_phase = cycle_length;
  61.     }
  62.     minor_phase -= 1;
  63.     return minor_phase;
  64. }
  65.